package com.android.thelastnoet.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.cardview.widget.CardView;

import com.android.thelastnoet.bean.Note;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class NoteMapper {

    private static NoteMapper noteMapper = null;
    private DBOpenHelper dbOpenHelper;
    private SQLiteDatabase db;

    public NoteMapper(Context context) {
        dbOpenHelper = DBOpenHelper.getDbOpenHelper(context);
    }

    public static synchronized NoteMapper getNoteMapper(Context context) {
        if (noteMapper == null) {
            noteMapper = new NoteMapper(context);
        }
        return noteMapper;
    }


    //获得所有便签 用于展示 “所有便签” 这个虚拟分组----除了私密便签
    public List<Note> getAll(int userID) {
        Log.d("mapper", String.valueOf(userID));
        List<Note> notes = new ArrayList<>();
        db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(DBOpenHelper.TABLE.NOTE, null, "groupName <> ? and groupName <> ? and userId = ?", new String[]{"废纸篓", "私密便签", String.valueOf(userID)}, null, null, null);
        while (cursor.moveToNext()) {
            Note note = new Note();
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String subContent = cursor.getString(cursor.getColumnIndex("subContent"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String groupName = cursor.getString(cursor.getColumnIndex("groupName"));
            String createTime = cursor.getString(cursor.getColumnIndex("createTime"));

            note.setId(id);
            note.setContent(content);
            note.setGroupName(groupName);
            note.setCreateTime(createTime);
            note.setTitle(title);
            note.setSubContent(subContent);
            note.setUserId(userID);
            Log.d("mapper", String.valueOf(note.getUserId()));
            notes.add(note);
        }
        cursor.close();
        return notes;
    }

    /*添加便签*/
    public long addNote(Note note) {
        db = dbOpenHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("title", note.getTitle());
        contentValues.put("subContent", note.getSubContent());
        contentValues.put("content", note.getContent());
        contentValues.put("createTime", note.getCreateTime());
        contentValues.put("groupName", note.getGroupName());

        long id = db.insert(DBOpenHelper.TABLE.NOTE, null, contentValues);
        return id;
    }

    /*修改便签*/
    public void updateNote(Note note) {
        db = dbOpenHelper.getWritableDatabase();
        Log.d("mapper", "id:" + note.getId());
        ContentValues contentValues = new ContentValues();
        contentValues.put("title", note.getTitle());
        contentValues.put("subContent", note.getSubContent());
        contentValues.put("content", note.getContent());
        contentValues.put("createTime", note.getCreateTime());
        contentValues.put("groupName", note.getGroupName());

        int update = db.update(DBOpenHelper.TABLE.NOTE, contentValues,
                " id = ?", new String[]{String.valueOf(note.getId())});
        Log.d("mapper", String.valueOf(update));
    }


    /*假删除便签*/
    public void deleteNote_F(int id) {
        db = dbOpenHelper.getWritableDatabase();
        Log.d("mapper", "id:" + id);
        ContentValues contentValues = new ContentValues();
        contentValues.put("groupName", "废纸篓");

        int update = db.update(DBOpenHelper.TABLE.NOTE, contentValues,
                " id = ?", new String[]{String.valueOf(id)});
        Log.d("mapper", String.valueOf(update));
    }

    /*删除便签*/
    public void deleteNote(Note note) {
        db = dbOpenHelper.getWritableDatabase();
        String id = String.valueOf(note.getId());
        db.delete(DBOpenHelper.TABLE.NOTE, "id = ?", new String[]{id});
    }


    //获得一个组的所有便签
    public LinkedList<Note> getNotesByGroup(String GroupName) {
        Log.d("mapper", GroupName);
        LinkedList<Note> notes = new LinkedList<Note>();
        db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(DBOpenHelper.TABLE.NOTE, null, "groupName = ?", new String[]{GroupName}, null, null, null);
        while (cursor.moveToNext()) {
            Note note = new Note();
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String subContent = cursor.getString(cursor.getColumnIndex("subContent"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String groupName = cursor.getString(cursor.getColumnIndex("groupName"));
            String createTime = cursor.getString(cursor.getColumnIndex("createTime"));

            note.setId(id);
            note.setContent(content);
            note.setGroupName(groupName);
            note.setCreateTime(createTime);
            note.setTitle(title);
            note.setSubContent(subContent);

            notes.add(note);
        }
        cursor.close();
        return notes;
    }

    /*根据id获得一条便签--用于测试*/
    public Note getNoteById(int id) {
        Note note = null;
        String id1 = Integer.toString(id);
        Cursor cursor = db.query(DBOpenHelper.TABLE.NOTE, null, "id = ?", new String[]{id1}, null, null, null);
        if (cursor != null) {
            note = new Note();
            /*！！！游标越界*/
            cursor.moveToFirst();
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String subContent = cursor.getString(cursor.getColumnIndex("subContent"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String groupName = cursor.getString(cursor.getColumnIndex("groupName"));
            String createTime = cursor.getString(cursor.getColumnIndex("createTime"));

            note.setId(id);
            note.setContent(content);
            note.setGroupName(groupName);
            note.setCreateTime(createTime);
            note.setTitle(title);
            note.setSubContent(subContent);

        } else {
            Log.d("根据id查找便签", "找不到");
        }
        return note;
    }

}
